package com.tb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tb.mapper.TeacherMapper;
import com.tb.pojo.Teacher;
import com.tb.pojo.User;
import com.tb.service.AllService;
import com.tb.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl extends AllService implements TeacherService {

    @Autowired
    private TeacherMapper tm;


    @Override
    public PageInfo<Teacher> findAll(int currentPage) {
        PageHelper.startPage(currentPage, 5);
        List<Teacher> teacherList = tm.findAll();
        /*处理数据*/
//        System.out.println(teacherList);
        PageInfo<Teacher> page = new PageInfo<Teacher>(teacherList);

        if (teacherList.size() != 0) {
            return page;
        }
//        System.out.println(page);
        return null;
    }

    @Override
    public boolean resign(Teacher tea) {
        Teacher teacher = tm.findByTid(tea.getTid(), tea.getEmail());
        if (teacher == null) {
            return false;
        }
        tm.deleteOne(teacher.getTid());
        /*每次执行删除时 需要刷新一下自增的情况*/
        tm.refresh();
        return true;
    }

    @Override
    public Teacher findOne(String tid) {
        return tm.findByTid(tid, null);
    }

    @Override
    public void modify(Teacher teacher) {
        tm.updateOne(teacher);
    }

    @Override
    public PageInfo<Teacher> findAllCondition(int college, int title, int currentPage) {
        Page<Teacher> es = PageHelper.startPage(currentPage, 5);
        List<Teacher> teacherList = tm.findAllCondition(college, title);
//        System.out.println(teacherList);
        PageInfo<Teacher> page = new PageInfo<>(teacherList);
        if (teacherList.size() != 0) {
            return page;
        }
        return null;
    }


    @Override
    public boolean addTea(Teacher teacher) {
        User user = new User(null, "0" + teacher.getCollege() + "00" + tm.getTotal(), teacher.getPassword(), null, null);
        user = us.register(user);
//        System.out.println(user);
        if (user != null) {
            /*教师的工号 = 所属学院的编号 + 在数据库中的总表*/
            teacher.setTid(user.getUsername());
//            System.out.println(teacher);
            tm.addTea(teacher);
            return true;
        }
        return false;
    }
}


